package co.yixiang.modules.monitor.rest;

import co.yixiang.modules.monitor.service.VisitsService;
import co.yixiang.utils.RequestHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 访问记录控制器，用于管理访问记录的相关操作
 */
@RestController
@RequestMapping("/api/visits")
@Api(tags = "系统:访问记录管理")
public class VisitsController {

    /**
     * 注入访问记录服务类，用于处理访问记录的业务逻辑
     */
    private final VisitsService visitsService;

    /**
     * 构造方法，初始化VisitsController
     * @param visitsService 访问记录服务类
     */
    public VisitsController(VisitsService visitsService) {
        this.visitsService = visitsService;
    }

    /**
     * 创建访问记录
     * 通过RequestHolder获取当前请求信息，并记录访问次数
     * @return ResponseEntity 表示创建成功的HTTP响应
     */
    @PostMapping
    @ApiOperation("创建访问记录")
    public ResponseEntity<Object> create(){
        visitsService.count(RequestHolder.getHttpServletRequest());
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 查询访问记录信息
     * @return ResponseEntity 包含访问记录信息的HTTP响应
     */
    @GetMapping
    @ApiOperation("查询")
    public ResponseEntity<Object> get(){
        return new ResponseEntity<>(visitsService.get(),HttpStatus.OK);
    }

    /**
     * 查询访问记录的图表数据
     * @return ResponseEntity 包含图表数据的HTTP响应
     */
    @GetMapping(value = "/chartData")
    @ApiOperation("查询图表数据")
    public ResponseEntity<Object> getChartData(){
        return new ResponseEntity<>(visitsService.getChartData(),HttpStatus.OK);
    }
}
